<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 快手控制器
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\KuaiShouModel;
use Exception;
use think\addons\Service;
use think\exception\PDOException;
use think\facade\Cache;

class KuaiShou extends Adminbase
{
    //初始化
    protected function initialize()
    {
        parent::initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $this->modelClass = new KuaiShouModel;
    }

    //后台首页
    public function index()
    {
        if ($this->request->isAjax()) {

            $limit = $this->request->get("limit/d", 999999);
            $page = $this->request->get("page/d", 1);
            $search = $this->request->get("search", '');
            $where[] = [
                KuaiShouModel::IS_DELETE, '=', 0,
            ];
            if ($search) {
                $searcharr = [
                    KuaiShouModel::ORDERNO,
                    KuaiShouModel::REALNAME,
                    KuaiShouModel::ID_NUMBER,
                    KuaiShouModel::BANK_CARD_NUMBER,
                    KuaiShouModel::PHONE,
                ];
                $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
            }

            $count = $this->modelClass
                ->where($where)
                ->count();

            $data = $this->modelClass
                ->where($where)
                ->order('id', 'DESC')
                ->page($page, $limit)
                ->select();
            $result = ["code" => 0, 'count' => $count, 'data' => $data];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 删除数据
     * @return void
     */
    public function del()
    {
        $ids = $this->request->param('id/a', null);
        if (empty($ids)) {
            $this->error('参数错误！');
        }
        if (!is_array($ids)) {
            $ids = [0 => $ids];
        }
        $where[] = [KuaiShouModel::ID, 'in', $ids];
        $list = KuaiShouModel::where($where)->select();
        $count = 0;
        $time = date('Y-m-d H:i:s');
        try {
            foreach ($list as $k => $v) {
                $count += $v->save([
                    KuaiShouModel::IS_DELETE => 1,
                    KuaiShouModel::DELETE_TIME => $time,
                ]);
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success("操作成功！");
        }
        $this->error('没有数据删除！');
    }

}
